package com.artwork.controller;

import com.artwork.common.domain.AjaxResult;
import com.artwork.common.enums.JsonResultEnum;
import com.artwork.common.exception.JsonException;
import com.artwork.model.User;
import com.artwork.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author JoJo
 * @Data 2021/9/8$ 13:53$
 * @Description
 * @Param $
 * @Return $
 */
@RestController
@RequestMapping("/admin")
public class UserController {
    @Autowired
    UserService userService;


    @PostMapping("/user/add")
    public AjaxResult addUser(User user){
        // todo 判断电话/处理密码/邀请码

        user.setCreateTime(System.currentTimeMillis());
        userService.addUser(user);
        return AjaxResult.success();
    }

    /**
     * @desc 修改用户积分
     * @param id
     * @param coin
     * @return
     * @throws JsonException
     */
    @PostMapping("/user/update")
    public AjaxResult updateUser(int id, BigDecimal coin) throws JsonException {
        User user = userService.getUserById(id);
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        // 修改用户积分
        user.setCoin(coin);
        userService.updateUser(user);
        return AjaxResult.success();
    }

    /**
     * @desc 获取我的下级用户
     * @param id
     * @return
     * @throws JsonException
     */
    @GetMapping("/user/son")
    public List<User> getSonUser(int id) throws JsonException{
        User user = userService.getUserById(id);
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        List<User> sonUsers = userService.getSonUser(id);
        return sonUsers;
    }

    /**
     * @desc 获取我的上级用户
     * @param fatherId
     * @return
     * @throws JsonException
     */
    @GetMapping("/user/father")
    public User getFatherUser(int fatherId) throws JsonException{
        User user = userService.getUserById(fatherId);
        if (user == null){
            throw new JsonException(JsonResultEnum.USER_NULL);
        }
        return user;
    }

}
